/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package vm;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Shape;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.BadLocationException;
import javax.swing.text.Highlighter.HighlightPainter;
import javax.swing.text.JTextComponent;

/**
 *
 * @author Peer
 */
public class vmHighlightPainter implements HighlightPainter {
    
    Color bgColor, frameColor, decoColor;
    int deco;
    final static int DECO_BOTH = 2;
    final static int DECO_TOP = 1;
    final static int DECO_BOTTOM = 0;
    final static int DECO_NONE = -1;

    public vmHighlightPainter(Color bgColor, Color frameColor, Color decoColor, int deco) {
        this.bgColor = bgColor;
        this.frameColor = frameColor;
        this.decoColor = decoColor;
        this.deco = deco;
    }

    public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c) {
        try {
            Rectangle b = bounds.getBounds();
            Rectangle r0 = c.modelToView(p0);
            Rectangle r1 = c.modelToView(p1);
            int[] xCoords = {r0.x+1,r0.x+(r1.x-r0.x)/2,r1.x};
            int[] yCoords = {r0.y,r0.y+(r1.x-r0.x)/2,r1.y};

            if(r1.x-r0.x <= 0) return;   //trying to mark a line break, we don't do that!
            
            if(bgColor!=null) {
                g.setColor(bgColor);
                g.fillRect(r0.x, r0.y, r1.x-r0.x, r1.height);
            }
            if(frameColor!=null) {
                g.setColor(frameColor);
                g.drawRect(r0.x, r0.y, r1.x-r0.x, r1.height-1);
            }
            if(decoColor!=null) {
                g.setColor(decoColor);
                switch(deco) {
                    case 2: g.fillPolygon(xCoords, yCoords, 3);
                    case 0: g.drawLine(r0.x+1, r0.y+r0.height-1, r1.x-1, r1.y+r0.height-1); break;
                    case 1: g.fillPolygon(xCoords, yCoords, 3);
                }
            }

        } catch (BadLocationException ex) {
            Logger.getLogger(hvm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
